/*
 * @Author: ShengXD <1519256482@qq.com>
 * @Date: 2024-11-07 16:21:24
 * @LastEditors: ShengXD <1519256482@qq.com>
 * @LastEditTime: 2024-11-07 16:43:33
 * @FilePath: /zonmind-om/apps/web-antd/src/utils/utils.ts
 * @Description: 过滤 undefined 参数
 */
export function extractAndReconstructUrl(url: string): string {
  // 正则表达式匹配查询参数
  const queryRegex = /([^?&=]+)=([^&]*)/g;
  const params: { [key: string]: string } = {};

  // 提取查询参数
  let match;
  while ((match = queryRegex.exec(url)) !== null) {
    const key = decodeURIComponent(match[1]??'');
    const value = decodeURIComponent(match[2]??'');
    if (value !== 'undefined') {
      params[key] = value;
    }
  }
  // 重新组合 URL
  const baseUrl = url.split('?')[0];

  if (!Object.keys(params).length) {
    return `${baseUrl}`;
  }

  return `${baseUrl}?${new URLSearchParams(params).toString()}`;
}
